package cn.gotom.asvg;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;

import org.apache.log4j.Logger;

import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;
import android.view.View;
import cn.gotom.svg.SVGDiagram;
import cn.gotom.svg.SVGException;

public class SVGView extends View
{
	protected final Logger log = Logger.getLogger(this.getClass());
	private final SVGDiagram svg = new SVGDiagram();

	public SVGView(Context context)
	{
		super(context);
	}

	@Override
	protected void onDraw(Canvas canvas)
	{
		Log.d("SVGView", "onDraw");
		//this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		svg.render(canvas);
		// canvas.drawPicture((Picture) svg.getComponent());
	}

	public void load(byte[] buffer)
	{
		Reader reader = new StringReader(new String(buffer));
		load(reader);
	}

	public void load(File file)
	{
		try
		{
			load(new FileInputStream(file));
		}
		catch (FileNotFoundException e)
		{
			log.error(e.getMessage(), e);
		}
	}

	public void load(int resId)
	{
		load(this.getResources().openRawResource(resId));
	}

	public void load(InputStream in)
	{
		Reader reader = new InputStreamReader(in);
		load(reader);
	}

	public void load(Reader reader)
	{
		((SVGDiagram) svg).setDebug(true);
		svg.load(reader);
	}

}
